package servlet;

import java.util.List;
import database.*;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = "/c")
public class CollegeServlet extends BaseServlet{
	static final int NUM = 3;
	public void page(HttpServletRequest req,HttpServletResponse resp) throws Exception{
		List<College> colist;
		List<Major> ma;
		List<MixCM> mix ;
		ma = DP.update(Major.class, "select * from major", null);
		colist = DP.update(College.class,"select * from college" , null);
		String sql = "select c.id,c.name as cname,m.name as mname from college c,major m where m.c_id=c.id order by c.id";
		mix = DP.fun1(MixCM.class, sql);
		int len = mix.size();
		int pagenum = len%3==0?len/3:len/3+1;
		sql += " limit 0,"+ NUM;
		mix = DP.fun1(MixCM.class, sql);
		req.setAttribute("pagenum", pagenum);
		req.setAttribute("page", 1);
		req.setAttribute("colist", colist);
		req.setAttribute("mix", mix);
		req.setAttribute("ma", ma);
		req.getRequestDispatcher("page/colist.jsp").forward(req, resp);
	}
	public void topage(HttpServletRequest req,HttpServletResponse resp) throws Exception{
		List<College> colist;
		List<Major> ma;
		ma = DP.update(Major.class, "select * from major", null);
		colist = DP.update(College.class,"select * from college" , null);
		List<MixCM> mix ;
		String sql = MixCM.sqls(req.getParameter("mname"), req.getParameter("cname"));
		Integer pagenum = Integer.parseInt(req.getParameter("pagenum"));
		Integer page;
		page = Integer.parseInt(req.getParameter("page"));
		sql += " limit "+(page-1)*NUM+","+NUM;
		mix = DP.fun1(MixCM.class, sql);
		req.setAttribute("pagenum", pagenum);
		req.setAttribute("page", page);
		req.setAttribute("colist", colist);
		req.setAttribute("mix", mix);
		req.setAttribute("ma", ma);
		req.setAttribute("mn", req.getParameter("mname"));
		req.setAttribute("cn", req.getParameter("cname"));
		req.getRequestDispatcher("page/colist.jsp").forward(req, resp);
	}
	public void select(HttpServletRequest req,HttpServletResponse resp) throws Exception{
		List<College> colist;
		List<Major> ma;
		ma = DP.update(Major.class, "select * from major", null);
		colist = DP.update(College.class,"select * from college" , null);
		List<MixCM> mix ;
		String sql = MixCM.sqls(req.getParameter("mname"), req.getParameter("cname"));
		mix = DP.fun1(MixCM.class, sql);
		int len = mix.size();
		Integer pagenum = len%3==0?len/3:len/3+1;
		System.out.println("现在的总页数为"+pagenum);
		Integer page;
		page = 1;
		sql += " limit "+(page-1)*NUM+","+NUM;
		mix = DP.fun1(MixCM.class, sql);
		req.setAttribute("pagenum", pagenum);
		req.setAttribute("page", page);
		req.setAttribute("colist", colist);
		req.setAttribute("mix", mix);
		req.setAttribute("ma", ma);
		req.setAttribute("mn", req.getParameter("mname"));
		req.setAttribute("cn", req.getParameter("cname"));
		req.getRequestDispatcher("page/colist.jsp").forward(req, resp);
	}
	public void update(HttpServletRequest req,HttpServletResponse resp) throws Exception{
		DP.update(College.class, 
		"update college set name='"+req.getParameter("name")+"' where id="+req.getParameter("id"), null);
	}
	public void insert(HttpServletRequest req,HttpServletResponse resp) throws Exception{
		String sql = "insert into college(name) values(?)";
		DP.update(College.class, sql, req.getParameter("name"));
	}
}
